import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(private jwtService: JwtService) {}
  private readonly users = [
    {
      id: 1,
      username: 'admin',
      password: 'admin123',
    },
    {
      id: 2,
      username: 'test',
      password: '123456',
    },
  ];
  async signIn(
    username: string,
    pass: string,
  ): Promise<any> {
    const user = await this.users.find((item) => item.username === username);
    console.log(user);
    if (!user) {
      throw new HttpException('无此账户', HttpStatus.INTERNAL_SERVER_ERROR);
    }
    if (pass !== user.password) {
      throw new HttpException('密码错误', HttpStatus.INTERNAL_SERVER_ERROR);
    }
    const { password, ...result } = user;
    const payload = { userId: result.id, username: result.username };
    try {
      return {
        accessToken: await this.jwtService.signAsync(payload),
        username: user.username,
        refreshToken: '',
      };
    } catch (err) {
      console.log('catch', err);
    }
  }
}

@Injectable()
export class MobileAuthService {

}